/**
 * @file logviewer.cpp
 * @brief LogViewer class methods implementation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 *
 * @author VestniK (Sergey N.Vidyuk) sir.vestnik@gmail.com
 * @date 6 Jul 2010
 */
#include "logviewer.h"

#include <QtCore/QString>

#include <QtGui/QListWidgetItem>
#include <QtGui/QIcon>

using namespace mjn;

LogViewer::LogViewer(QWidget* parent): QDialog(parent)
{
    setupUi(this);
    listWidget->setWordWrap(true);
    connect( log(),SIGNAL(newMessage(LogMessage)),
             this,SLOT(newMessage(LogMessage)) );
}

void LogViewer::newMessage(const LogMessage& message)
{
    QListWidgetItem *item = new QListWidgetItem();
    /// @todo add different icons for different types
    QString type;
    switch (message.type()) {
        case Logger::Network :
            type = tr("Network log message");
            item->setIcon(QIcon(":/icons/network-message.svg"));
            break;
        case Logger::Protocol :
            type = tr("Protocol log message");
            item->setIcon(QIcon(":/icons/protocol-message.svg"));
            break;
        case Logger::Session :
            type = tr("Session log message");
            item->setIcon(QIcon(":/icons/session-message.svg"));
            break;
        case Logger::Game :
            type = tr("Game log message");
            item->setIcon(QIcon(":/icons/game-message.svg"));
            break;
        case Logger::Other :
            type = tr("Log message of unlnown type");
            break;
    }
    item->setToolTip(type);
    item->setText(QString("%1 %2")
            .arg(message.time().toString("yyyy-MM-dd hh:mm:ss.zzz"))
            .arg(message.message()));
    switch (message.severity()) {
        case Logger::Error : item->setTextColor(Qt::red);      break;
        case Logger::Debug : item->setTextColor(Qt::darkGray); break;
        case Logger::Info  : item->setTextColor(Qt::darkBlue); break;
    }
    listWidget->addItem(item);
}
